package com.yswy.backstage.service.impl;

import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.dto.req.TgChannleListVo;
import com.yswy.backstage.entity.user.User;
import com.yswy.backstage.service.TgChannleChildService;
import com.yswy.backstage.service.TgChannleService;
import com.yswy.common.utils.HttpUtil;
import com.yswy.common.utils.StringUtils;
import com.yswy.domain.common.BasePojo;
import com.yswy.domain.entity.PackageManage;
import com.yswy.domain.entity.TgChannle;
import com.yswy.domain.entity.TgChannleChild;
import com.yswy.domain.mapper.TgChannleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class TgChannleServiceImpl extends ServiceImpl<TgChannleMapper, TgChannle>
        implements TgChannleService {
    static String spileCode = "-";

    @Value("${zjym}")
    private String zjym;

    @Resource
    private TgChannleChildService tgChannleChildService ;
    @Override
    public Page<TgChannle> listTgChannle(TgChannleListVo tgChannleListVo) {
        Page<TgChannle> page = page(new Page<>(tgChannleListVo.getPageNum(), tgChannleListVo.getPageSize()),
                new LambdaQueryWrapper<TgChannle>()
                        .eq(TgChannle::getIsDel , 0)
                        .like(StringUtils.isNotEmpty(tgChannleListVo.getChannle()) , TgChannle::getChannle , tgChannleListVo.getChannle())
                        .ge(tgChannleListVo.getStaTime()!= null ,TgChannle::getCreateTime , tgChannleListVo.getStaTime())
                        .le(tgChannleListVo.getEndTime()!= null ,TgChannle::getCreateTime , tgChannleListVo.getEndTime())
                        );
        List<TgChannle> tgChannles = page.getRecords();
        for (TgChannle tgChannle :tgChannles){
            tgChannle.setExaminePrice(tgChannle.getExaminePrice()/100);
        }
        page.setRecords(tgChannles);
        return page;
    }

    @Override
    public void addOrUpdateTgChannle(TgChannle tgChannle, User user) {
        tgChannle.setExaminePrice(tgChannle.getExaminePrice()*100);
        if(tgChannle.getId() != null){
            tgChannle.setCreateBy(user.getId().longValue());
            tgChannle.setCreateTime(new Date());
            List<TgChannleChild> tgChannleChildList = tgChannleChildService.list(new LambdaQueryWrapper<TgChannleChild>().eq(TgChannleChild::getTgChannleId ,tgChannle.getId() ));
            for (TgChannleChild tgChannleChild :tgChannleChildList){
                String[] strings = tgChannleChild.getChannle().split(spileCode);
                tgChannleChild.setChannle(tgChannle.getChannle()+spileCode+strings[1]);
                String urlMsg = "https://aiimg.zyykj168.com/tg/handleCallback?&type=2&channle="+ tgChannleChild.getChannle()+
                        "&ADVERTISER_ID=__ADVERTISER_ID__&CSITE=__CSITE__&CONVERT_ID=__CONVERT_ID__&IMEI=__IMEI__&ANDROIDID=__ANDROIDID__&OAID=__OAID__&CALLBACK_PARAM=__CALLBACK_PARAM__&OS=__OS__&MAC=__MAC__&IP=__IP__&MODEL=__MODEL__";
                tgChannleChild.setUrlMsg(urlMsg);
            }
            tgChannleChildService.updateBatchById(tgChannleChildList);


            HttpUtil.doGet(zjym+"tg/delDeduction?channle="+tgChannle.getChannle());

        }else {
            Integer integer = count(new LambdaQueryWrapper<TgChannle>().eq(TgChannle::getChannle ,tgChannle.getChannle())
                    .eq(BasePojo::getIsDel,0));
            if (integer>0){
                throw new IllegalStateException("平台已存在");
            }
            tgChannle.setUpdateBy(user.getId().longValue());
            tgChannle.setUpdateTime(new Date());
        }

        saveOrUpdate(tgChannle);
    }

    @Override
    public void delTgChannle(Long Id, User user) {
        TgChannle tgChannle = getById(Id);
        tgChannle.setIsDel(1);
        tgChannle.setUpdateBy(user.getId().longValue());
        tgChannle.setUpdateTime(new Date());
        updateById(tgChannle);
    }
}
